<?php
namespace app\payqr\model;
use app\BaseModel;
class CardStatistics extends BaseModel
{
    protected $name = 'longbing_card_statistics';
    public function getUserid($where){
        return $this->where($where)->find();
    }
    public function addinfo($data){
        return $rest = $this->insert($data);
    }
    public function updateinfo($where,$data){
        return $rest = $this->where($where)->update($data);
    }
    public function getlist($user_list,$uniacid,$type = 0){
        if($type==1){
            $de = '=';
        }else{
            $de = 'in';
        }
        $where1 = [
            ['user_id',$de,$user_list],
            ['sign','=','view'],
            ['uniacid','=',$uniacid],
            ['type','in','1,2,3,6'],
        ];
        $where2 = [
            ['user_id',$de,$user_list],
            ['sign','=','praise'],['uniacid','=',$uniacid],
            ['type','in','3,4'],
        ];
        $where3 = [
            ['user_id',$de,$user_list],
            ['sign','=','copy'],['uniacid','=',$uniacid],
            ['type','in','1,2,4,7,8'],
        ];
        $data = $this->where([array_filter($where1)])->whereor([array_filter($where2)])->whereor([array_filter($where3)])
            ->field('sum(number) as number,sign,type')
            ->order('number','desc')
            ->group('sign,type')
            ->select()
            ->toArray();
        if($data){
            $maxnumber = $data[0]['number'];
            foreach ($data as $key=>$val){
                $data[$key]['rate'] = round($val['number']/$maxnumber* 100, 2);
                $data[$key]['name'] = $this->getNameinfo($val['sign'],$val['type']);
            }
        }
        return $data;
    }

    public function getweeklist($where){
        $weekday = date("Y-m-d",strtotime("-7 day"));
         $week = $this->where($where)->whereWeek('create_time', $weekday)->sum('number');
        return $week;
    }
    public function getCustomerCount($where){
        return $this->where($where)->sum('number');
    }
    //新增咨询
    public function zxInfo($user_id,$where=[],$type=0){
        if($type==1){
            $de = '=';
        }else{
            $de = 'in';
        }
        $where1 = [
            ['user_id',$de,$user_id],
            ['sign','=','copy'],
            ['type','=','8'],
            $where
        ];
        $where2 = [
            ['user_id',$de,$user_id],
            ['sign','=','praise'],
            ['type','=','5'],
            $where
        ];
        $count = $this->where([array_filter($where1)])->whereor([array_filter($where2)])->count();
        return $count;
    }
}